<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends CommonController {

	/**
	 * [index 管理员页面显示]
	 */
	public function index(){

		$admin = M('admin');
		if(!empty($_GET['search_username'])){
			//有关键字
			$where = "a.username like '%".$_GET['search_username']."%'";
		}else{
			$where = '';
		}

		// 定义每页显示条目
		$num = I('get.prepage')?I('get.prepage'):5;
		$count = $admin->alias('a')->where($where)->count();

		// 实例化分页类 传入总记录数和每页显示的记录数
		$Page = new \Think\Page($count,$num);

		//获取limit参数
		$limit = $Page->firstRow.','.$Page->listRows;

		// 查询zz_user表中基本字段信息, 分配给模板展示
		$res = $admin->alias('a')->join('zz_auth_group ag on a.group_id=ag.id')->field('a.admin_id, a.username, a.login_time, a.admin_name, a.login_ip, a.group_id, ag.title')->where($where)->limit($limit)->select();

		// 分页显示输出
		$pages = $Page->show();
		$allpage = $Page->totalPages;
		$p = $_GET['p']?$_GET['p']:1;

		if ($p==$allpage) {
			$rows =  $count - ($allpage-1)*$num;
		}else{
			$rows = $num;
		}

		// 分配一些页码信息
		$this->assign('total',$count);
		$this->assign('rows',$rows);
		$this->assign('allpage',$allpage);
		$this->assign('p',$p);
		$this->assign('pages',$pages);
		$this->assign('res', $res);
		$this->assign('test','管理员列表');
		// 管理组列表
		$group = M('auth_group');
		$group_list = $group->select();
		$this->assign('group_list', $group_list);
		$this->display();
	}

	/**
	 * [add 管理员添加]
	 */
	public function add(){

		// 获得管理组列表
		$group = M('auth_group');
		$group_list = $group->select();
		$this->assign('group_list', $group_list);
		$this->assign('test','添加管理员');
		$this->display();
	}

	/**
	 * [insert 管理员添加处理]
	 */
	public function insert(){
		if (IS_POST) {
			// 处理表单数据
			$_POST['password'] = md5(I('post.password'));// 默认密码为用户名

			$m = M();
			$m->startTrans();
			$admin = M('admin');
			$admin->create();

			$id = $admin->add();

			if ($id) {
				$data['group_id'] = I('post.group_id');
				$data['uid'] = $id;
				$auth_group = M('auth_group_access');
				$res = $auth_group->add($data);
				if ($res) {
					$m->commit();
					$this->success('添加成功', U('index'));

				}else{
					$m->rollback();
					$this->error('添加失败');

				}


			} else {
				$m->rollback();
				$this->error('添加失败');
			}

		}else{
			E('你访问的页面不存在');
		}
	}

	/**
	 * [edit 普通管理员编辑页面]
	 */
	public function edit(){
		$id = $_GET['id'];
		$admin = M('admin');
		$res = $admin->where(array('admin_id'=>$id))->select();
		$group = M('auth_group')->select();

		$this->assign('res', $res);
		$this->assign('group_list', $group);
		$this->display();
	}

	/**
	 * [editdo 普通管理员编辑处理页面]
	 */
	public function editdo(){

			$repass = I('post.repass');
			if ($repass == 1) {
				$_POST['password'] = md5($_POST['username']);
			}
			$m = M();
			$m->startTrans();

			$admin = M('admin');
			$admin->create();
			$res = $admin->save();
			if ($res) {
				$data['group_id'] = I('post.group_id');
				$id = I('post.admin_id');
				$auth_group = M('auth_group_access');
				$re = $auth_group->where("uid='$id'")->save($data);

				if ($re) {
					$m->commit();
					$this->success('修改成功',U('index'));
				}else{
					$m->rollback();
					$this->error('修改失败');
				}
			} else {
				$m->rollback();
				$this->error('修改失败');
			}
	}

	/**
	 * [delete 普通管理员删除页面]
	 */
	public function delete(){
		$admin_id = I('get.id');
		$p = I('get.p');
		$m = M();
		$m->startTrans();

		$admin = M('admin');
		$res = $admin->delete($admin_id);

		// $res = 0;
		if ($res) {
			$group = M('auth_group_access');
			$re = $group->where("uid='$admin_id'")->delete();
			// echo $group->_sql();die;
			if ($re) {
				$m->commit();
				$this->redirect('index',array('p'=>$p));

			} else {
				$m->rollback();
				$this->error('删除失败',U('index'));
			}

		} else {
			$m->rollback();
			$this->error('删除失败',U('index'));
		}
	}

	/**
	 * [change 处理双击用户名,昵称,下拉框管理发送的ajax]
	 */
	public function change(){

		if (IS_AJAX) {
			// 修改用户名, 发送的ajax处理
			if (isset($_POST['username']) && !empty($_POST['username'])) {

				$admin = M('admin');
				$admin->create();
				$res = $admin->save();
				if ($res) {
					$this->ajaxReturn('1');
				} else {
					$this->ajaxReturn('0');
				}

			// 修改昵称, 发送的ajax处理
			}else if(isset($_POST['admin_name']) && !empty($_POST['admin_name'])){

				$admin = M('admin');
				$admin->create();
				$res = $admin->save();
				if ($res) {
					$this->ajaxReturn('1');
				} else {
					$this->ajaxReturn('0');
				}

			// 修改管理员组, 发送的ajax处理
			}else if(isset($_POST['group_id']) && !empty($_POST['group_id'])){
				$m = M();
				$m->startTrans();

				$admin = M('admin');
				$admin->create();
				$res = $admin->save();
				// $res = 0;
				if ($res) {
					$data['group_id'] = I('post.group_id');
					$id = I('post.admin_id');

					$auth_group = M('auth_group_access');
					$re = $auth_group->where("uid='$id'")->save($data);

					if ($re) {
						$m->commit();
						$m = M('auth_group')->find($_POST['group_id']);
						$this->ajaxReturn($m['title']);
						// $this->success('修改成功',U('index'));
					}else{
						$m->rollback();
						$this->ajaxReturn('0');
					}

				} else {
					$this->ajaxReturn('0');
				}
			}
		}else{
			$m->rollback();
			$this -> error('非法操作');
		}
	}

	/**
	 * [getTitle 获得原来该管理员的管理员组]
	 */
	public function getTitle(){
		$group = M('auth_group');
		$res = $group->find($_POST['group_id']);
		$this->ajaxReturn($res['title']);

	}

}
